package com.huike.game.Activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.huike.game.R;

import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

import androidx.appcompat.app.AppCompatActivity;

public class GameActivity69QC extends AppCompatActivity {

    private ImageView iv_left_image;
    private TextView tv_left_text;
    private TextView tv_content_title;
    private ImageView iv_rigth_image;
    private TextView tv_right_text;
    private TextView iv_game69qc_name;
    private ImageView iv_game69qc_image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_activity69_qc);
        initView();

        Intent intent = getIntent();
        final String val = intent.getStringExtra("game69_val");


//        图片长安
        iv_game69qc_image.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                iv_game69qc_name.setVisibility(View.VISIBLE);
                iv_game69qc_name.setText("付款项目:故宫  付款金额:" +val);
                return true;
            }
        });

        //定时刷新二维码

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {

                            Qc(iv_game69qc_image,"http://www.baidu.com",500,500);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });

            }
        },0,5000);

    }

    private void Qc(ImageView imageView, String s, int w, int h) throws WriterException {
        HashMap<EncodeHintType, String> map = new HashMap<>();
        map.put(EncodeHintType.CHARACTER_SET,"utf-8");

        BitMatrix encode = new QRCodeWriter().encode(s, BarcodeFormat.QR_CODE,w,h);
        int[] ints = new int[w * h];
        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                if(encode.get(i,j)){
                    ints[i * w + j] =Color.BLACK;
                }else{
                    ints[i * w + j] =Color.WHITE;

                }
            }
        }

        Bitmap bm = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
        bm.setPixels(ints,0,w,0,0,w,h);
        imageView.setImageBitmap(bm);

    }


    private void initView() {
        iv_left_image = (ImageView) findViewById(R.id.iv_left_image);
        tv_left_text = (TextView) findViewById(R.id.tv_left_text);
        tv_content_title = (TextView) findViewById(R.id.tv_content_title);
        iv_rigth_image = (ImageView) findViewById(R.id.iv_rigth_image);
        tv_right_text = (TextView) findViewById(R.id.tv_right_text);

        tv_content_title.setText("支付界面");
        tv_right_text.setVisibility(View.GONE);
        tv_left_text.setVisibility(View.GONE);
        iv_rigth_image.setVisibility(View.GONE);
        iv_left_image.setImageResource(R.drawable.tiao);

        iv_game69qc_name = (TextView) findViewById(R.id.iv_game69qc_name);
        iv_game69qc_image = (ImageView) findViewById(R.id.iv_game69qc_image);
    }


}
